/**
      = Hello Vocabulary @2010 =
	version 0.2.10
http://code.google.com/p/hello-vocabulary/
******************************************************

Copyright (c) 2010 Mead, laiqinyi@gmail.com


@ MIT License:Licensing
http://www.opensource.org/licenses/mit-license.php
@ Creative Commons License
http://creativecommons.org/licenses/by/2.5/legalcode
**/

//its a id attri for SPAN tag. so won't render twice.
 var LIGHT_VERSION_TITLE="hv_0112";
 
 // ==Option==
 //Font_size
 //Bold or weight
 //Color select...
var font_size = 16;

//constor.
var Highlighter = function(colors) {  
 this.colors = colors;  
 if (this.colors == null) {  
  this.colors = ['#ffff00,#FF00FF','#FF69B4,#000000','#eabcf4,#0000CD',  
  '#c8e5ef,#008000','#f3e3cb, #FF4500', '#e7cfe0,#4B0082',  
  '#c5d1f1,#FF0000','#deeee4, #FF00FF','#b55ed2,#000000',  
   '#c5d1f1,#FF0000','#87CEFA, #FF0000','#B0E0E6,#FF00FF', 
    '#5F9EA0,#DC143C','#00FFFF, #0000FF','#b55ed2,#000000', 
  '#dcb7a0,#333333', '#7983ab,#FF4500', '#6894b5, #FF6347'];  
 }  
}  
  
//invoke the color action. public.
Highlighter.prototype.highlight = function(node, keywords) {  
 if (!keywords || !node || !node.nodeType || node.nodeType != 1)  
  return;  
   
 keywords = this.parsewords(keywords);  
 if (keywords == null)  
  return;  
   
 for (var i = 0; i < keywords.length; i++) {  
  this.colorword(node, keywords[i]);  
 }  
}  
 
 
//color a sigle word.
Highlighter.prototype.colorword = function(node, keyword) {  	
 for (var i = 0; i < node.childNodes.length; i++) {  
  var childNode = node.childNodes[i];      
  if (childNode.nodeType == 3) {      
   //childNode is #text  	
   var re = new RegExp(keyword.word, 'i');  
   if (childNode.data.search(re) == -1) continue;   
   re = new RegExp('(' + keyword.word + ')', 'gi');  
   var forkNode = document.createElement('span');  
   forkNode.innerHTML = childNode.data.replace(re, '<span id='+LIGHT_VERSION_TITLE
											   +' style="font-weight:bold; font-size:'
											   +font_size+'px; background-color:'+keyword.bgColor
											   +';color:'+keyword.foreColor+'" mce_style="background-color:'
											   +keyword.bgColor+';color:'+keyword.foreColor+'">$1</span>');  
   node.replaceChild(forkNode, childNode);  
  } else if (childNode.nodeType == 1) {  
  	if(childNode.id != null && childNode.id != "" && childNode.id == LIGHT_VERSION_TITLE){
			//because it has rendered already.
			//console.log("got U" + childNode.id+"="+childNode.innerHTML);
			continue;
		}
   //childNode is element  
   this.colorword(childNode, keyword);  
   
  }  
 }  
}  
  
//split the key word and allocate a color...
Highlighter.prototype.parsewords = function(keywords) {  
 keywords = keywords.replace(/\s+/g, ' ');  
 keywords = keywords.split(' ');  
 if (keywords == null || keywords.length == 0)  
  return null;  
   
 var results = [];  
 for (var i = 0; i < keywords.length; i++) {  
  var keyword = {};  
  var color = this.colors[i % this.colors.length].split(',');  
  keyword.word = keywords[i];  
  keyword.bgColor = color[0];  
  keyword.foreColor = color[1];  
  results.push(keyword);  
 }  
 return results;  
}  
  
 
Highlighter.prototype.sort = function(list) {  
 list.sort(function(e1, e2) {  
  return e1.length < e2.length;  
 });  
}  


